<template>
  <view class="content">
    <view class="mainbox">
      <view class="interior" @tap="gorulesCenter">
        <view>用户协议</view>
        <view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <view class="interior" @tap="gorulesCenter2">
        <view>隐私政策</view>
        <view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <!-- <view class="interior">
        <view>隐私政策</view>
        <view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view> -->
      <view class="interior" @tap="godisclaimer">
        <view>免责申明</view>
        <view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <view @tap.stop="check" class="interior">
        <view>检测更新</view>
        <view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <view class="interior" @tap="quitlogin" >
        <view>退出登录</view>
        <view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <view @tap="gotologout" class="interiortwo">
        <view>注销账号</view>
        <view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
    </view>
    <u-modal v-model="checkshow" :show-cancel-button="true" :show-title="false" @confirm="confirm"  @cancel="cancel">
      <view v-if="!statusshow">
        <view v-if="!showcancel" style="padding: 30rpx 15rpx;" class="u-update-content">
					当前已为最新版本（{{ clientVersion }}），无需更新~
				</view>
        <view v-else style="padding: 30rpx 15rpx;">
          发现新版本~是否更新
        </view>
      </view>
      <view v-else style="padding: 30rpx 15rpx;">
        正在下载更新，请等待~
      </view>
		</u-modal>
  </view>
</template>

<script>
import { getVersion,downloadCertificate } from '@/api/common'
import checkver from '../package.json'
import { logOff } from '@/api/main'
export default {
  data() {
    return {
      checkshow:false,
      clientVersion:null,
      downloadUrl:null,
      showcancel:false,
      statusshow:false,
    };
  },
  
  methods: {
    gotologout() {
      uni.navigateTo({
        url: '/pagesMine/logout',
      })
    },
    // 退出登录
    quitlogin() {
      let token = uni.getStorageSync('token')
      let phone = uni.getStorageSync('phone')
      console.log('看看本地', token)
      let obj = {
        phone: phone
      }
      logOff(obj).then(res => {
        console.log('退出登录', res)
        if (res.code == 200) {
          uni.removeStorageSync('consigneeId')
          uni.removeStorageSync('token')
          uni.removeStorageSync('latAndLong')
          uni.removeStorageSync('address')
          uni.reLaunch({ url: '/pages/login/login' })
        }
      })
    },
    check(){
      let system = uni.getSystemInfoSync();
      console.log(system)
      const platform = system.platform;
      console.log(checkver.version);
      const obj = {
        version: checkver.version,
        appId: platform === 'android' ? 1 : 2
      };
      getVersion(obj).then(res => {
        this.clientVersion = res.data.clientVersion;
        this.downloadUrl = res.data.downloadUrl;
        this.checkshow = true;
        if (this.clientVersion !== checkver.version) {
          this.showcancel = true;
        }
      });
    },  
    cancel(){
      this.checkshow=false
    },
    confirm(){
      console.log('确认',this.statusshow)
      if(this.statusshow){
        return
      }
      if(this.clientVersion!=checkver.version){  
          this.statusshow=true
		  const fileExtension = this.downloadUrl.split('.').pop().toLowerCase();
		  console.log('fileExtension',fileExtension);
		   if (fileExtension === 'ipa') {
				this.statusshow=false
					var url = 'https://apps.apple.com/us/app/援亲软件/id6738318034';
					// 使用系统默认浏览器打开App Store页面
					plus.runtime.openURL(url);
				return;
		    }else{
				const downloadTask = uni.downloadFile({
					url:this.downloadUrl,
					success: (res) => {
					  console.log('下载成功',res)
					  this.statusshow=false
					  //安装
					  plus.runtime.install(
						res.tempFilePath, {
						  force: true
						},
						function(_res) {
						  plus.runtime.restart();
						}
					  )
					},
					fail: (err)=> {
					  uni.$u.toast('下载失败')
					}
				  })
			    downloadTask.onProgressUpdate((res) => {
				    console.log('下载进度' + res.progress)
				    this.progress=res.progress
			    })
			}
      }
    },
    godisclaimer() {
      uni.navigateTo({
        url: '/pagesMine/rulesCenter?type=3'
      })
    },
    //隐私协议
    gorulesCenter() {
      uni.navigateTo({
        url: '/pagesMine/rulesCenter?type=1'
      })
    },
    gorulesCenter2() {
      uni.navigateTo({
        url: '/pagesMine/rulesCenter?type=2'
      })
    },
    // dianping(){
    //   downloadCertificate().then(res=>{
    //     console.log('成功',res.data)
    //     const fileUrl = 'https://testfile.ihasl.com/file/download4/4557855df2fb4ecf83fafa351497d7b9.pdf';
    //     uni.downloadFile({
    //             url: fileUrl,
    //             success: (res) => {
    //                 if (res.statusCode === 200) {
    //                     uni.saveFile({
    //                         tempFilePath: res.tempFilePath,
    //                         success: (saveRes) => {
    //                           console.log('文件保存成功',saveRes);  
    //                             uni.showToast({
    //                                 title: '文件下载成功',
    //                                 icon:'success'
    //                             });
    //                             console.log('文件保存路径:', saveRes.savedFilePath);
    //                                 const filePath = saveRes.savedFilePath;
    //                                 wx.openDocument({
    //                                     filePath: filePath,
    //                                     success: () => {
    //                                         console.log('文件打开成功');
    //                                     },
    //                                     fail: (err) => {
    //                                         console.error('文件打开失败', err);
    //                                     }
    //                                 });
    //                         },
    //                         fail: (err) => {
    //                             uni.showToast({
    //                                 title: '保存文件失败',
    //                                 icon: 'none'
    //                             });
    //                             console.error(err);
    //                         }
    //                     });
    //                 }
    //             },
    //             fail: (err) => {
    //                 uni.showToast({
    //                     title: '文件下载失败',
    //                     icon: 'none'
    //                 });
    //                 console.error(err);
    //             }
    //         });
    //   }).catch((err) => {
    //     console.log('失败',err)
    //   })
    // }
  }
};
</script>

<style scoped lang="scss">
.content {
  width: 100vw;
  height: 100vh;
  background-color: #F5F5F5;
  overflow: hidden;

  .mainbox {
    border-radius: 8rpx;
    margin: 20rpx 32rpx 0 32rpx;
    background-color: white;
    padding: 16rpx 24rpx;

    .interior {
      display: flex;
      justify-content: space-between;
      padding: 24rpx 0;
      align-items: center;
      border-bottom: 1rpx solid #F5F5F5;
    }

    .interiortwo {

      display: flex;
      justify-content: space-between;
      padding: 24rpx 0;
      align-items: center;
    }
  }
}
</style>